package face.pay.task.sharing.service.impl;


import face.pay.dao.SharingAccountDao;
import face.pay.entity.SharingMchAccount;
import face.pay.task.sharing.service.IsharingAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class SharingAccountImpl implements IsharingAccountService {


    @Autowired
    private SharingAccountDao sharingAccountDao;

    /**
     * 添加分账对象
     */
    @Override
    public void addSharingAccount() {
        //查找应该添加market的分账关系
        List<SharingMchAccount> MarketAccountList = this.sharingAccountDao.queryNewMarketAccount();
        log.info("本次查询到应配market分账对象共 : {} 条", MarketAccountList.size());
        if (MarketAccountList.size() > 0) {
            List<SharingMchAccount> insertMarketList = MarketAccountList.stream().map(m -> {
                m.setStatus(0);
                m.setRelationType("PARTNER");
                return m;
            }).collect(Collectors.toList());
            this.sharingAccountDao.insertlist(insertMarketList);
            log.info("本次插入market分账对象成功 共 : {} 条", insertMarketList.size());
        }

        //查找应该添加的cpn分账关系
        List<SharingMchAccount> cpnAccountList = this.sharingAccountDao.queryNewCPNAccount();
        log.info("本次查询到应配cpn分账对象共 : {} 条", cpnAccountList.size());
        if (cpnAccountList.size() > 0) {
            List<SharingMchAccount> insertCpnList = cpnAccountList.stream().map(c -> {
                c.setStatus(0);
                c.setRelationType("PARTNER");
                return c;
            }).collect(Collectors.toList());
            this.sharingAccountDao.insertlist(insertCpnList);
            log.info("本次插入cpn分账对象成功 共 : {} 条", insertCpnList.size());
        }


        //查找应该添加的oem分账关系
        List<SharingMchAccount> oemAccountList = this.sharingAccountDao.queryNewOemAccount();
        log.info("本次查询到应配oem分账对象共 : {} 条", oemAccountList.size());
        if (oemAccountList.size() > 0) {
            List<SharingMchAccount> insertOemList = oemAccountList.stream().map(o -> {
                o.setStatus(0);
                o.setRelationType("PARTNER");
                return o;
            }).collect(Collectors.toList());
            this.sharingAccountDao.insertlist(insertOemList);
            log.info("本次插入oem分账对象成功 共 : {} 条", insertOemList.size());
        }
    }
}
